/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jlibrarian.web;

import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import jlibrarian.CustomerManagerImpl;
import jlibrarian.DbManager;
import jlibrarian.entities.Customer;
import jlibrarian.interfaces.CustomerManager;

/**
 *
 * @author dandelion8
 */
public class CreateCustomerServlet extends HttpServlet {
   
    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        // initialize
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        
        // glue in errors
        final String glue = ", ";
        
        
 
        Map<String,String> params = new TreeMap<String,String>();
        params.put("firstName", "jméno");
        params.put("surname", "příjmení");
        //params.put("born", "narozen");
        params.put("city", "město");
        params.put("street", "ulice a č.p.");
        params.put("telephone", "telefon");
        
        // errors
        StringBuilder errorCache = new StringBuilder();
        
        // add values
        Map<String,String> values = new HashMap<String,String>();
        for(Entry<String,String> p : params.entrySet()){
            String value = request.getParameter(p.getKey());
            if(value == null || value.length() == 0){
                errorCache.append(p.getValue()).append(glue);
            }
            values.put(p.getKey(), value);
        }
       

        // mh
        MessagesHandler mh = new MessagesHandler(session);
        
        // set error message
        if(errorCache.length() > 0)
        {
             // store values
            session.setAttribute("customer-values", values);
            // set error
            mh.setMessage("error", "Nutné vyplnit položky: " + errorCache.substring(0, errorCache.length()-glue.length()) + ".");
            // redirecting back
            response.sendRedirect(request.getContextPath() + "/customer-create.jsp");
            
            
            return;
        }
        // delete values
        session.setAttribute("customer-values", null);
            
        // OK
        mh.setMessage("status", "Zákazník byl vytvořen.");
            
        // creating
        Customer c = new Customer();
        c.setCity(values.get("city"));
        c.setStreet(values.get("street"));
        c.setFirstName(values.get("firstName"));
        c.setSurname(values.get("surname"));
        c.setTelephone(values.get("telephone"));
        
        Calendar cal = Calendar.getInstance();
        cal.clear();
        c.setBorn(cal);
        
        CustomerManager cm = new CustomerManagerImpl(DbManager.getDataSource());
        cm.createCustomer(c);
        
        response.sendRedirect(request.getContextPath() + "/customer-list.jsp");
            
    }
}
